/**
  ******************************************************************************
  * @file    Devices_E28.c
  * @author  AnshininTakaha
  * @version V1.23
  * @date		 2021/12/28
  * @brief   The File of E22(RF) Init Modify
	* @copyright (c) 2017-2021 JZI. All rights reserved.
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "Devices_E28.h"

#include "Devices_E22.h"
#include "Devices_Command.h"
#include "Devices_Receiver.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


/* Exported mention ----------------------------------------------------------*/
void E28_USART_IT_Register(UART_HandleTypeDef* huartx);
void E28_RF_Setting(uint16_t Address,uint8_t Channel);
void E28_USART_ReBaudRate(uint32_t BaudRate);



/* Exported variables --------------------------------------------------------*/
/* E28 Functions */
E28_Fnc E28_Function = E28_FunctionsGround_Init;
#undef E28_FunctionsGround_Init

/* Exported functions --------------------------------------------------------*/
/**
 * @brief USART1 Register init function of E22 Module.
 * @param void.
 * @return void.
 */
void E28_USART_IT_Register(UART_HandleTypeDef* huartx)
{
	/* Clear the Flag First for Prevent IDLE Flag Broken Init */
   __HAL_UART_CLEAR_IDLEFLAG(huartx);
	
	/* Open the Serial port Using Polling */
  __HAL_UART_ENABLE(huartx);
	
	/* Receive IT Enable: DMARx */
	__HAL_UART_ENABLE_IT(huartx,UART_IT_IDLE);
	
	#ifdef LIDAR_USE_CODE
	/* Activation the first Receive */
	HAL_UART_Receive_DMA(huartx, \
											(uint8_t*)LID_AT_CommandReceive, \
											AT_CommandLength);
	#endif
	
	#ifdef RECEIVE_USE_CODE
	/* Activation the first Receive */
	HAL_UART_Receive_DMA(huartx, \
											(uint8_t*)Receiver_LidarPusherReceive.Lidar_MsgReceive, \
											Lidar_MsgReceiveLength);
	#endif
	
	/* Transmit IT Enable: DMATx */
  //None..
	
	/* USART Register */
	E28_Function.E28_Register = huartx;
}



/**
 * @brief E28 Module setting function.
 * @param Address  The E28 Module confirm Address
 * @param Channel  The E28 Module confirm Channel
 * @attention The E28 Address & Channel must be the same.
							Both Transfer Node(s) and Receive Node(s).
 * @return void.
 */
void E28_RF_Setting(uint16_t Address,uint8_t Channel)
{
	/* Store for Reset & Setting Data */
	uint8_t Soft_Reset[6] = {0};
	
	/* Get Default Setting from Grand */
	E28_Setting_t E28Setting = {E2G4_CONFIG_PACK};
	
	/* Set E28 Into Setting Mode */
	DLC_MD0_HIGH();
	DLC_MD1_HIGH();
	DLC_MD2_HIGH();
	HAL_Delay(200);
	
	/* Change Custom Modify Address & Channal */
	E28Setting.AddressH = (Address >> 8) & 0xFF;
	E28Setting.AddressL = Address & 0xFF;
	E28Setting.Chan = Channel;
	
	#ifdef LIDAR_USE_CODE
	/* Send Data for Setting E28 and waitting for callback */
	
	do
	{
		HAL_UART_Transmit_DMA(E28_Function.E28_Register, \
												(uint8_t*)&E28Setting.E28_SettingMessage, \
												6);
		HAL_Delay(200);
		
	}while((uint8_t)LID_AT_CommandReceive[0] != 0xC0);
	#endif
	
	#ifdef RECEIVE_USE_CODE
	/* Send Data for Setting E28 and waitting for callback */
	do
	{
		HAL_UART_Transmit_DMA(E28_Function.E28_Register, \
												(uint8_t*)&E28Setting.E28_SettingMessage, \
												6);
		HAL_Delay(200);
	}while((uint8_t)Receiver_LidarPusherReceive.Lidar_MsgReceive[0] != 0xC0);
	#endif
	
	
	
	/* Soft Reset */
	Soft_Reset[0] = 0xC4;
	Soft_Reset[1] = 0xC4;
	Soft_Reset[2] = 0xC4;
	while(HAL_UART_Transmit_DMA(E28_Function.E28_Register, \
														 (uint8_t*)&Soft_Reset, \
														 3) == HAL_BUSY);
	HAL_Delay(200);
	
	/* Set E22 Into Working Mode */
	DLC_MD0_LOW();
	DLC_MD1_LOW();
	DLC_MD2_HIGH();
	HAL_Delay(200);
	
}


/**
 * @brief USART1 Reinitialization function of E22 Module(Working Baudrate).
 * @param void.
 * @return void.
 */
void E28_USART_ReBaudRate(uint32_t BaudRate)
{
	/* Stop the USART and Delink all the partner prevent usart Error */
	/* Keep the Instance first before deinit */
	USART_TypeDef *KeepInstance = E28_Function.E28_Register->Instance;

	/* Deinit */
	HAL_UART_DeInit(E28_Function.E28_Register);
	
	//ReSet BaudRate..
	E28_Function.E28_Register->Instance = KeepInstance;
  E28_Function.E28_Register->Init.BaudRate = BaudRate;
  E28_Function.E28_Register->Init.WordLength = UART_WORDLENGTH_8B;
  E28_Function.E28_Register->Init.StopBits = UART_STOPBITS_1;
  E28_Function.E28_Register->Init.Parity = UART_PARITY_NONE;
  E28_Function.E28_Register->Init.Mode = UART_MODE_TX_RX;
  E28_Function.E28_Register->Init.HwFlowCtl = UART_HWCONTROL_NONE;
  E28_Function.E28_Register->Init.OverSampling = UART_OVERSAMPLING_16;
	
	//Link the partner again..
  if (HAL_UART_Init(E28_Function.E28_Register) != HAL_OK)
  {
    Error_Handler();
  }
}





/* End -----------------------------------------------------------------------*/
